package com.byd.dms.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.byd.dms.entity.User2;
import com.byd.dms.enums.Status;
import com.byd.dms.enums.StatusDeserializer;
import com.byd.dms.enums.StatusSerializer;
import org.junit.jupiter.api.Test;

public class StatusDemo {
    public static void main(String[] args) {
        serialize();
    }
    
    @Test
    public void deserializer2() {
        String jsonStr = "20";
        Long test1 = JSON.parseObject(jsonStr, Long.class);
        System.out.println(test1 + ", " + test1.getClass().getName());
        User2 user = new User2("张小娴", 22, "女");
        user.setMyStatus(Status.Completed);
        System.out.println("user = " + user);
        System.out.println("user json = " + JSON.toJSONString(user));
        
        // com.alibaba.fastjson.JSONException: parse enum com.byd.dms.enums.Status error, value : 10
        String userJson = "{\"age\":22,\"map\":[],\"myStatus\":10,\"sex\":\"女\",\"username\":\"韩梅梅\"}";
        userJson = "{\"age\":22,\"map\":[],\"myStatus\":\"Completed\",\"sex\":\"女\",\"username\":\"张小娴\"}";
        
        User2 user2 = JSON.parseObject(userJson, User2.class);
        System.out.println(user2.getMyStatus());
        System.out.println("user2  = " + user2);
    }
    
    private static void deserializer() {
        String jsonStr = "20";
        ParserConfig.getGlobalInstance().putDeserializer(Status.class, new StatusDeserializer());
        Long test1 = JSON.parseObject(jsonStr, Long.class);
        System.out.println(test1 + ", " + test1.getClass().getName());
        Status status = JSON.parseObject(jsonStr, Status.class);
        System.out.println("status = " + status + " , " + status.name());
    
        
    
        String userJson = "{\"age\":22,\"map\":[],\"myStatus\":10,\"sex\":\"女\",\"username\":\"韩梅梅\"}";
        
        User2 user2 = JSON.parseObject(userJson, User2.class);
        System.out.println(user2.getMyStatus());
        System.out.println("user2  = " + user2);
    }
    
    
    private static void serialize() {
        SerializeConfig config = new SerializeConfig();
        config.put(Status.class, new StatusSerializer());
        
        
        String jsonStr = JSON.toJSONString(Status.Completed, config);
        System.out.println("jsonStr = " + jsonStr);
        
        Status completed = Status.Completed;
        System.out.println("Status.Completed " + completed);
        
        System.out.println("==============================");
        
        User2 user2 = new User2("韩梅梅", 22, "女");
        user2.setMyStatus(Status.Ready);
        String userJson = JSON.toJSONString(user2, config);
        System.out.println("userJson = " + userJson);
    }
    
    
}
